package com.bdqn.business.access.dao;

import com.bdqn.entity.access.Permission;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PermissionRepository extends JpaRepository<Permission,String>, JpaSpecificationExecutor<Permission> {

    List<Permission> findByPidAndType(String pid, Integer type);

    @Query(nativeQuery = true , value = "SELECT COUNT(r.id) FROM pe_role r,pe_permission p ,pe_role_permission rp WHERE p.id = rp.permission_id AND rp.role_id = r.id AND p.id = ?")
    int getRolesByPermissionId(String id);


    @Query(nativeQuery = true,value = "SELECT p.* FROM pe_user u,pe_user_role ur,pe_role r, pe_role_permission rp,pe_permission p WHERE p.id = rp.permission_id AND rp.role_id = r.id AND r.id = ur.role_id AND ur.user_id = u.id AND u.id = ?")
    List<Permission> getPermissionListByUserId(@Param("userId") String userId);
}
